import { reactive } from 'vue'
import { getProductOptionList, getProductDataForm } from '@/api/base/product'
import { getStoreOptionsList } from '@/api/base/shop'
import {
  addSaleReturnData,
  auditSaleReturnOrder,
  getSaleReturnDataForm,
  updateSaleReturnData
} from '@/api/warehouse/sale-return-order'
import { getSaleScanDetail } from '@/api/warehouse/sale-return-order/sale-detail-index'
import { getReturnScanDetail } from '@/api/warehouse/sale-return-order/return-detail-index'
import { getWarehouseOptionList } from '@/api/base/warehouse'
export function useDetail(getFormValues: Function) {
  const status = reactive({
    saving: false,
    loading: false
  })

  const createOrUpdate = async () => {
    const values = getFormValues()
    if (status.saving) return false
    status.saving = true
    try {
      values?.code ? await updateSaleReturnData(values.code, values) : await addSaleReturnData(values)
      status.saving = false
      return true
    } catch (err) {
      status.saving = false
      return false
    }
  }

  // 根据编码获取产品详情
  const getProductByCode = async (code: string) => {
    return getProductDataForm(code)
  }

  // 获取产品下拉列表
  const getProductOptions = async () => {
    return getProductOptionList()
  }

  // 获取销售采购单
  const getDataById = async (userCode: any) => {
    return getSaleReturnDataForm(userCode)
  }

  //获取门店列表
  const getShopList = async () => {
    return getStoreOptionsList()
  }

  const getWarehouseList = async () => {
    return getWarehouseOptionList()
  }

  //审核订单
  const auditOrder = async (code: string, param: any) => {
    return auditSaleReturnOrder(code, param)
  }

  // 获取销售扫码详情
  const getSaleScanDetailList = async (code: any) => {
    return getSaleScanDetail(code)
  }

  // 获取退货扫码详情
  const getReturnScanDetailList = async (code: any) => {
    return getReturnScanDetail(code)
  }

  return {
    status,
    createOrUpdate,
    getDataById,
    getShopList,
    getWarehouseList,
    getProductOptions,
    getProductByCode,
    auditOrder,
    getSaleScanDetailList,
    getReturnScanDetailList
  }
}
